package com.oms.app.order.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oms.app.order.model.Order;
import com.oms.app.order.service.IOrderService;
import com.oms.page.PageModel;
import com.oms.util.DataDeal;

/**
 * 订单相关业务
 * controller
 * 
 */
@RequestMapping("")
public class OrderAction {
	
	private IOrderService orderService;

	public void setOrderService(IOrderService orderService) {
		this.orderService = orderService;
	}
	
	//绑定时间格式
	@InitBinder  
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat, true));
    }
	
	//跳转到订单管理页面
	@RequestMapping(value="/order_orderManagerList/{shopId}")
	public String enterOrderList(@PathVariable String shopId, HttpServletRequest request) {
		request.setAttribute("shopId", shopId);
		return "manager/order/orderManagerList";
	}
	
	//获取订单列表
	@ResponseBody
	@RequestMapping(value="/order_getList/{shopId}")
	public Map<String, Object> getOrderList(@PathVariable String shopId, HttpServletRequest request) {
		request.setAttribute("shopId", shopId);
		List<Order> list =  orderService.getOrderList(request);
		return PageModel.getListToString(list, request);
	}
	
	//审核订单
	@ResponseBody
	@RequestMapping(value="/order_auditOrder", method=RequestMethod.POST)
	public Map<String, Object> auditOrder(HttpServletRequest request) {
		String orderIds = DataDeal.clearNull(request.getParameter("orderId")), 
			   status = DataDeal.clearNull(request.getParameter("isPass"), "1");
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if (orderIds.length() > 0 && status.length() > 0) {
			if (orderService.auditOrder(orderIds, status)) {
				jsonMap.put("success", true);
				jsonMap.put("msg", "操作成功！");
			} else {
				jsonMap.put("success", false);
				jsonMap.put("msg", "操作失败！");
			}
		} else {
			jsonMap.put("success", false);
			jsonMap.put("msg", "操作失败！");
		}
		return jsonMap;
	}
	
	// 删除订单信息
	@ResponseBody
	@RequestMapping(value="/order_deleteOrder/{orderId}")
	public Map<String, Object> deleteOrder(@PathVariable String orderId, HttpServletRequest request) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if (orderService.deleteOrder(orderId)) {
			jsonMap.put("success", true);
			jsonMap.put("msg", "删除订单成功！");
		} else {
			jsonMap.put("success", false);
			jsonMap.put("msg", "删除订单失败！");
		}
		return jsonMap;
	}
	
	//订单详情页面
	@RequestMapping(value="/order_orderDetails/{orderId}")
	public String orderDetails(@PathVariable String orderId, HttpServletRequest request) {
		request.setAttribute("orderId", orderId);
		return "manager/order/orderDetails";
	}
	
	//获取订单详情列表
	@ResponseBody
	@RequestMapping(value="/order_getDetailList/{orderId}")
	public Map<String, Object> getOrderDetailList(@PathVariable String orderId, HttpServletRequest request) {
		request.setAttribute("orderId", orderId);
		List<Map<String, Object>> list =  orderService.getOrderDetailList(orderId);
		return PageModel.getListToString(list, request);
	}
	
	//改变订单详情状态
	@ResponseBody
	@RequestMapping(value="/order_changeDetailStatus/{orderId},{detailId},{status}")
	public Map<String, Object> changeDetailStatus(@PathVariable String orderId, @PathVariable String detailId, 
			@PathVariable String status, HttpServletRequest request) {
		request.setAttribute("orderId", orderId);
		//是否为商家
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if (orderService.changeDetailStatus(detailId, (("1".equals(status)) ? "2" : "1"))) {
			jsonMap.put("success", true);
			jsonMap.put("msg", "操作成功！");
		} else {
			jsonMap.put("success", false);
			jsonMap.put("msg", "操作失败！");
		}
		return jsonMap;
	}
	
	//删除订单详情信息
	@ResponseBody
	@RequestMapping(value="/order_deleteOrderDetails/{orderId},{detailId}")
	public Map<String, Object> deleteOrderDetails(@PathVariable String orderId, @PathVariable String detailId, HttpServletRequest request) {
		request.setAttribute("orderId", orderId);
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if (orderService.delOrderDetailsByPK(detailId)) {
			jsonMap.put("success", true);
			jsonMap.put("msg", "操作成功！");
		} else {
			jsonMap.put("success", false);
			jsonMap.put("msg", "操作失败！");
		}
		return jsonMap;
	}
}